package com.ridbor001.demo.dao;

import com.ridbor001.demo.bean.RDBAdmin;
import com.ridbor001.demo.util.RDBDataStore;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

public class RDBAdminDAO {
    RDBDataStore dataStore = new RDBDataStore();
    // 检查教师是否存在
    public   boolean AdminExists(String AdminId) throws SQLException {
        String sql = "SELECT COUNT(*) FROM admins WHERE admin_id = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, AdminId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        }
        return false;
    }
    // 添加
    public   void addAdmin(RDBAdmin Admin) throws SQLException {
        String sql = "INSERT INTO Admins (" +
                "admin_id, name, password, gender, age, phone" +
                ") VALUES (?, ?, ?, ?, ?, ?)";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, Admin.getAdminId());
            pstmt.setString(2, Admin.getName());
            pstmt.setString(3, Admin.getPassword());
            pstmt.setString(4, Admin.getGender());
            pstmt.setInt(5, Admin.getAge());
            pstmt.setString(6,Admin.getPhone());
            pstmt.executeUpdate();
        }
    }
    // 根据教师ID和密码查管理员
    public Optional<RDBAdmin> findAdminByIdAndPassword(String AdminId, String password)
            throws SQLException {

        String sql = "SELECT * FROM admins WHERE admin_id = ? AND password = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, AdminId);
            pstmt.setString(2, password);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return Optional.of(new RDBAdmin(
                            rs.getString("name"),
                            rs.getString("password"),
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("phone"),
                            rs.getString("Admin_Id")
                    ));
                }
            }
        }
        return Optional.empty();
    }
    public   RDBAdmin findAdminById(String adminId) throws SQLException {
        final String sql = "SELECT admin_id, name, password, gender, age, phone "
                + "FROM Admins WHERE admin_id = ?";

        try (Connection conn = dataStore.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, adminId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return new RDBAdmin(
                            rs.getString("name"),
                            rs.getString("password"),
                            rs.getString("gender"),
                            rs.getInt("age"),
                            rs.getString("phone"),
                            rs.getString("Admin_id")
                    );
                }
            }
        }
        return null;
    }
}
